package com.ccys.cms.service.corporateAndInsurance.impl;

import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitDeeplinkService;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitIdentifierService;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitSService;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.corporateAndInsurance.PlanBenefitDto;
import com.ccys.db.dto.corporateAndInsurance.PlanBenefitPageDto;
import com.ccys.db.entity.TblPlanBenefit;
import com.ccys.db.entity.TblPlanBenefitDeeplink;
import com.ccys.db.entity.TblPlanBenefitIdentifier;
import com.ccys.db.entity.TblPlanBenefitService;
import com.ccys.db.mapper.TblPlanBenefitMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.vo.corporateAndInsurance.PlanBenefitVo;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PlanBenefitServiceImpl extends BaseServiceImpl<TblPlanBenefit> implements PlanBenefitService {

    @Resource
    private TblPlanBenefitMapper mapper;

    @Resource
    private PlanBenefitSService benefitSService;

    @Resource
    private PlanBenefitIdentifierService identifierService;

    @Resource
    private PlanBenefitDeeplinkService deeplinkService;


    @Override
    public ResponseResult getPage(PlanBenefitPageDto dto) {
        PageDto.startPage();
        List<PlanBenefitVo> list = mapper.getList(dto);
        PageInfo<PlanBenefitVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    public void deleteBenefit(String planId) {
        mapper.deleteBenefit(planId);
    }

    @Override
    public ResponseResult change(PlanBenefitDto dto) {
        TblPlanBenefit planBenefit = dto.getPlanBenefit();
        List<TblPlanBenefitService> serviceList = dto.getServiceList();
        List<TblPlanBenefitIdentifier> identifierList = dto.getIdentifierList();
        List<TblPlanBenefitDeeplink> deeplinkList = dto.getDeeplinkList();
        if (StringUtils.isEmpty(planBenefit.getId())) {
            //如果是新增
            serviceList.forEach(x -> x.setId(IdUtil.getSnowflakeNextIdStr()));
            //identifier
            if (CollectionUtils.isNotEmpty(identifierList)) {
                identifierList.forEach(x -> x.setId(IdUtil.getSnowflakeNextIdStr()));
            }
            //deeplink
            if (CollectionUtils.isNotEmpty(deeplinkList)) {
                deeplinkList.forEach(x -> x.setId(IdUtil.getSnowflakeNextIdStr()));
            }
        } else {
            String id = planBenefit.getId();
            if (CollectionUtils.isNotEmpty(serviceList)) {
                //修改删除之前的benefitService
                benefitSService.deleteBenefitService(id);
            }
            //identifier
            if (CollectionUtils.isNotEmpty(identifierList)) {
                identifierService.deleteIdentifier(id);
            }
            //deeplink
            if (CollectionUtils.isNotEmpty(deeplinkList)) {
                deeplinkService.deleteDeeplink(id);
            }
        }
        int i = this.insertOrUpdate(planBenefit);
        serviceList.forEach(x -> x.setBenefitId(planBenefit.getId()));
        benefitSService.insertAll(serviceList);

        // Identifier type
        // 1:AIA corporate;2:Direct corporate;3:AIA Healthshield;4:Subscription
        // 当 plan 是 Direct corporate 或者 Subscription package时 需要处理新增 Identifier
        // 1:email matching 2:shared identifier 3:unique identifier - auto generate 4:unique identifier - custom 5:deeplink
        // 当 Identifier type 是 1 时候保存多个email patter 到 tbl_plan_benefit_identifier，
        // 当 Identifier type 是 2 时要保存一条identifier 到 tbl_plan_benefit_identifier，
        // 当 Identifier type 是 5 时要保存多条 deeplink 到 tbl_plan_benefit_deeplink（3，4类型先不管）
        if (CollectionUtils.isNotEmpty(identifierList)) {
            identifierService.insertAll(identifierList);
        }

        if (CollectionUtils.isNotEmpty(deeplinkList)) {
            deeplinkService.insertAll(deeplinkList);
        }

        return ResponseResult.update(i);
    }
}
